﻿/*-----------------------------------------------------------------------------!
! Supplier Front end class                                                     !
!------------------------------------------------------------------------------!
! System   : Inventory Management System                                       !
! Client   : UK based                                                          !
! Date     : May 2011                                                        !
! Copyright: (C) KAWISOFT Inc                                                  !
! Program  : SupplierManager.cs                                                !
! Function : controls supplier front end functions                             !
!------------------------------------------------------------------------------!
! Notes:                                                                       !
! Original author: Md. Shaiful Islam                                           !
===============================================================================*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model.Poribar.SupplierPanjeri;
using Model.Upadan;
using Model.Panjeri;
using Model.Sohaok;
using Model.Sadharon;
using InventorySys.controller;
using System.Globalization;
using System.Web.UI.WebControls;

namespace InventorySys.view
{
    public partial class Supplier : Form
    {
        #region all private variable
        /// <summary>
        /// all private variable
        /// </summary>
        private SupplierController supplierController;
        private AddressController addressController;
        private Address address;
        private Model.Upadan.Supplier supplier;
        private Result result;
        private static string _pageMode = "";
        #endregion

        #region supplier constructor
        /// <summary>
        /// supplier constructor
        /// </summary>
        public Supplier()
        {
            InitializeComponent();
            // load country list
            loadCountryList();
        }
        #endregion



        #region load coutnry list
        /// <summary>
        /// load country list
        /// </summary>
        private void loadCountryList()
        {
            try
            {
                CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
                SortedDictionary<string, string> regionLookup = new SortedDictionary<string, string>(StringComparer.Ordinal);
                foreach (CultureInfo cultInfo in cultures)
                {
                    RegionInfo regionInfo = new RegionInfo(cultInfo.LCID);
                    if (!regionLookup.ContainsKey(regionInfo.EnglishName))
                    {
                        regionLookup.Add(regionInfo.EnglishName, regionInfo.TwoLetterISORegionName);
                        sp_Country.Items.Add(new ListItem(regionInfo.EnglishName, regionInfo.TwoLetterISORegionName));
                    }
                }
            }
            catch (Exception e) { }
        }
        #endregion

        #region save supplier details
        /// <summary>
        /// addMode add functionality for supplier page
        /// </summary>
        /// <returns></returns>
                
        private string saveSupplierDetails()
        {
            Model.Upadan.Supplier suppler = new Model.Upadan.Supplier();
            Model.Upadan.Address address = new Model.Upadan.Address();
            Result result = new Result();

            // set supplier information
            suppler.AccountReference = sp_AccountReference.Text;
            suppler.CompanyName = sp_CompanyName.Text;
            suppler.ContactPerson = sp_ContactPerson.Text;

            // set address information
            address.Country = sp_Country.Text;
            address.Email = sp_Email.Text;
            address.Fax = sp_Fax.Text;
            address.Mobile = sp_Mobile.Text;
            address.PostCode = sp_PostCode.Text;
            address.Telephone = sp_Telephone.Text;
            address.Web = sp_Web.Text;
            address.Description = sp_Address.Text;

            SupplierController spManager = new SupplierController();
            result = spManager.addNewSupplier(suppler, address);
            MessageBox.Show(result.Message);
            _pageMode = "viewMode";
            return suppler.Id;
        }
        #endregion

        #region update supplier details
        /// <summary>
        /// update supplier details
        /// </summary>
        /// <returns></returns>

        private string updateSupplierDetails()
        {
            // reset all objects
            supplier = new Model.Upadan.Supplier();
            address = new Address();
            result = new Result();

            // set supplier information
            //supplier.Id = label_ID.Text.ToString();
            supplier.AccountReference = sp_AccountReference.Text;
            supplier.CompanyName = sp_CompanyName.Text;
            supplier.ContactPerson = sp_ContactPerson.Text;

            // set address information
            address.Country = sp_Country.Text;
            address.Email = sp_Email.Text;
            address.Fax = sp_Fax.Text;
            address.Mobile = sp_Mobile.Text;
            address.PostCode = sp_PostCode.Text;
            address.Telephone = sp_Telephone.Text;
            address.Web = sp_Web.Text;
            address.Description = sp_Address.Text;
            address.PersonId = supplier.Id;
            
            SupplierController supplierController = new SupplierController();
            result = supplierController.updateSupplierDetails(supplier, address);
            MessageBox.Show(result.Message);
            _pageMode = "viewMode";
            return supplier.Id;
        }
        #endregion

        #region view supplier details
        /// <summary>
        /// view supplier details
        /// </summary>
        /// <param name="id"></param>
        private void supplierDetailsViewMode(string id)
        {
            loadSupplierDetails(id);
            hideEditMode();
            showViewMode();
        }
        #endregion

        #region supplier details edit mode
        /// <summary>
        /// supplier details edit mode
        /// </summary>
        /// <param name="id"></param>
        private void supplierDetailsEditMode(string id)
        {
            loadSupplierDetails(id);
            showEditMode();
            hideViewMode();
        }
        #endregion

        #region load supplier details into page element
        /// <summary>
        /// load supplier details in view & edit element
        /// </summary>
        /// <param name="supplierId"></param>
        private void loadSupplierDetails(string supplierId)
        {
            supplierController = new SupplierController();
            result = supplierController.GetSupplierDetails(supplierId);
            addressController = new AddressController();

            if (result.IsSuccess)
            {
                Model.Upadan.Supplier suppler = new Model.Upadan.Supplier();
                suppler = (Model.Upadan.Supplier)result.ResultObj;

                // header information
                label_ID.Text = suppler.Id;
                label_CNAME.Text = suppler.CompanyName;

                // details
                // view element
                labelAccountRef.Text = suppler.AccountReference;
                labelCompanyName.Text = suppler.CompanyName;
                labelContactPerson.Text = suppler.ContactPerson;

                // edit element
                sp_AccountReference.Text = suppler.AccountReference;
                sp_CompanyName.Text = suppler.CompanyName;
                sp_ContactPerson.Text = suppler.ContactPerson;

                // get address details by supplier id
                result = null;
                result = addressController.GetAddressDetails(suppler.Id);
                if (result.IsSuccess)
                {
                    address = (Address)result.ResultObj;

                    // view element
                    labelAddress.Text = address.Description;
                    labelEmail.Text = address.Email;
                    labelFax.Text = address.Fax;
                    labelMobile.Text = address.Mobile;
                    labelPostcode.Text = address.PostCode;
                    labelTelephone.Text = address.Telephone;
                    labelWeb.Text = address.Web;
                    labelCountry.Text = address.Country;

                    // edit element
                    sp_Address.Text = address.Description;
                    sp_Email.Text = address.Email;
                    sp_Fax.Text = address.Fax;
                    sp_Mobile.Text = address.Mobile;
                    sp_PostCode.Text = address.PostCode;
                    sp_Telephone.Text = address.Telephone;
                    sp_Web.Text = address.Web;
                    sp_Country.Text = address.Country;

                }
            }

        }
        #endregion

        
        
        
        // all dependencies
        
        #region decide page mode
        /// <summary>
        /// decide page mode
        /// </summary>
        /// <param name="pageMode"></param>
        /// <param name="_id"></param>
        public void decidePageMode(string pageMode, string _id)
        {
            _pageMode = pageMode;
            if (_pageMode.Equals("addMode"))
            {
                hideViewMode();
                sp_BtnCancel.Enabled = false;
            }
            else if (_pageMode.Equals("viewMode"))
            {
                supplierDetailsViewMode(_id);
            }
        }
        #endregion

        #region hide view mode
        /// <summary>
        /// hide view mode
        /// </summary>
        private void hideViewMode()
        {
            labelAccountRef.Hide();
            labelAddress.Hide();
            labelCompanyName.Hide();
            labelContactPerson.Hide();
            labelCountry.Hide();
            labelEmail.Hide();
            labelFax.Hide();
            labelMobile.Hide();
            labelPostcode.Hide();
            labelTelephone.Hide();
            labelWeb.Hide();

            // hide related button
            sp_BtnEdit.Hide();
            sp_BtnPrint.Hide();

        }
        #endregion        

        #region show view mode
        /// <summary>
        /// show view mode
        /// </summary>
        private void showViewMode()
        {
            labelAccountRef.Show();
            labelAddress.Show();
            labelCompanyName.Show();
            labelContactPerson.Show();
            labelCountry.Show();
            labelEmail.Show();
            labelFax.Show();
            labelMobile.Show();
            labelPostcode.Show();
            labelTelephone.Show();
            labelWeb.Show();
        }
        #endregion

        #region hide edit mode
        /// <summary>
        /// hide edit mode
        /// </summary>
        private void hideEditMode()
        {
            sp_AccountReference.Hide();    
            sp_Address.Hide();
            sp_CompanyName.Hide();
            sp_ContactPerson.Hide();
            sp_Country.Hide();
            sp_Email.Hide();
            sp_Fax.Hide();
            sp_Mobile.Hide();
            sp_PostCode.Hide();
            sp_Telephone.Hide();
            sp_Web.Hide();
            // hide related button
            sp_BtnAdd.Hide();
            sp_BtnCancel.Hide();
            // show related button
            sp_BtnEdit.Show();
            sp_BtnPrint.Show();

        }
        #endregion

        #region show edit mode
        /// <summary>
        /// show edit mode
        /// </summary>
        private void showEditMode()
        {
            sp_AccountReference.Show();
            sp_Address.Show();
            sp_CompanyName.Show();
            sp_ContactPerson.Show();
            sp_Country.Show();
            sp_Email.Show();
            sp_Fax.Show();
            sp_Mobile.Show();
            sp_PostCode.Show();
            sp_Telephone.Show();
            sp_Web.Show();
            // show related button
            sp_BtnAdd.Show();
            sp_BtnCancel.Show();
            sp_BtnCancel.Enabled = true;
            // hide related button
            sp_BtnEdit.Hide();
            sp_BtnPrint.Hide();
        }
        #endregion



        /// button handler section

        #region add button
        /// <summary>
        /// add button handler section
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sp_BtnAdd_Click(object sender, EventArgs e)
        {
            string supplierId = "";

            if (_pageMode.Equals("addMode"))
            {
                if (isSupplierOK())
                {
                    supplierId = saveSupplierDetails();
                    if (supplierId != null || supplierId != "")
                    {
                        supplierDetailsViewMode(supplierId);
                    }
                }
            }
            else if(_pageMode.Equals("viewMode"))
            {
                if (isSupplierOK())
                {
                    supplierId = updateSupplierDetails();
                    if (supplierId != null || supplierId != "")
                    {
                        supplierDetailsViewMode(supplierId);
                    }
                }
            }
        }
        #endregion

        #region edit button 
        /// <summary>
        /// edit button handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sp_BtnEdit_Click(object sender, EventArgs e)
        {
            supplierDetailsEditMode(label_ID.Text.Trim());
        }
        #endregion

        #region button close
        /// <summary>
        /// button close event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sp_BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region cancel button
        /// <summary>
        /// cancel button handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sp_BtnCancel_Click(object sender, EventArgs e)
        {
            supplierDetailsViewMode(label_ID.Text);
        }
        #endregion


        // validation section
        #region validation
        /// <summary>
        /// validation of required element
        /// </summary>
        /// <returns></returns>
        private bool isSupplierOK()
        {
            bool isOK = true;
            supplierErrorProvider.Clear();

            if (sp_AccountReference.Text == string.Empty)
            {
                supplierErrorProvider.SetError(sp_AccountReference, "Please enter account reference");
                isOK = false;
            }
            if (sp_CompanyName.Text == string.Empty)
            {
                supplierErrorProvider.SetError(sp_CompanyName, "Please enter company name");
                isOK = false;
            }
            if (sp_ContactPerson.Text == string.Empty)
            {
                supplierErrorProvider.SetError(sp_ContactPerson, "Please enter contact person");
                isOK = false;
            }
            if (sp_Address.Text == string.Empty)
            {
                supplierErrorProvider.SetError(sp_Address, "Please enter address");
                isOK = false;
            }
            if (sp_Country.Text == string.Empty)
            {
                supplierErrorProvider.SetError(sp_Country, "Please enter country");
                isOK = false;
            }
            if (sp_Mobile.Text == string.Empty)
            {
                supplierErrorProvider.SetError(sp_Mobile, "Please enter mobile no");
                isOK = false;
            }



            return isOK;
        }
        #endregion



    }
}
